package risk.control.application.component.msg;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import risk.control.application.R;


/**
 * CreationTime:2025/4/23
 * Describe:
 * Author:hm
 **/

public class ShowMsgDialog extends Dialog implements DialogInterface.OnDismissListener {

    //    private static final int width = 250;
//    private static final int height = 170;
    private static final int style = R.style.Dialog;
    private static final boolean isCancelable = false;

    private final View mRootView;
    private TextView mTvStr;
    private Button mBtnConfirm;
    private TextView mTvTitle;
    private ConfirmClickListener mConfirmClickListener;

    public ShowMsgDialog(Context context) {
        super(context, style);
        mRootView = View.inflate(context, R.layout.display_info_dialog, null);
        setContentView(mRootView);
        setCancelable(isCancelable);
        setCanceledOnTouchOutside(isCancelable);
        Window window = getWindow();
        if (window != null) {
            // 设置对话框的位置：居中
            WindowManager.LayoutParams params = window.getAttributes();
            //获取屏幕的宽高
            WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            Display display = windowManager.getDefaultDisplay();
            DisplayMetrics displayMetrics = new DisplayMetrics();
            display.getMetrics(displayMetrics);
            int width = displayMetrics.widthPixels;
            int height = displayMetrics.heightPixels;
//            float density = getDensity(context);
//            params.width = (int) (width * density);
//            params.height = (int) (height * density);
            //根据手机屏幕像素动态宽高
            params.width = (int) (width * 0.70);
            params.height = (int) (height * 0.5);
            params.gravity = Gravity.CENTER;
            window.setAttributes(params);
            initView();
            initData();
            initListener(this);
        }
    }


    private void initView() {
        mTvStr = mRootView.findViewById(R.id.tv_str);
        mTvTitle = mRootView.findViewById(R.id.tv_title);
        mBtnConfirm = mRootView.findViewById(R.id.btn_confirm);
    }

    private void initData() {

    }

    private void initListener(ShowMsgDialog displayInfoDialog) {
        mBtnConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mConfirmClickListener != null) {
                    mConfirmClickListener.onPositiveClick();
                }
                displayInfoDialog.dismiss();
            }
        });
    }

    private static float getDensity(Context context) {
        Resources resources = context.getResources();
        DisplayMetrics dm = resources.getDisplayMetrics();
        return dm.density;
    }

    @Override
    public void onDismiss(DialogInterface dialog) {

    }

    public void setBtnClickListener(ConfirmClickListener listener) {
        this.mConfirmClickListener = listener;
    }

    public void setCancelable(boolean isCancelable) {
        setCanceledOnTouchOutside(isCancelable);
    }

    public void serTitle(String text) {
        mTvTitle.setText(text);
    }

    public void setBtnText(String text) {
        mBtnConfirm.setText("确认");
    }

    public void showDialog(String str) {
        mTvStr.setText(str);
        this.show();
    }


}
